﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Xml.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

using DevExpress.Xpf.Editors.Settings;
using DevExpress.Xpf.Grid;

using Microsoft.SharePoint.Client;

using SBPweb.RIA.Client.Common.Collections;

namespace SBPweb.Sharepoint.Client.Controls.Fields
{

    public partial class ChoiceEditSettings : ComboBoxEditSettings, IFieldEditSettings<FieldChoice>
    {

        private static Dictionary<Guid, ObservableList<KeyValuePair<int, string>>> fieldChoiceCache = new Dictionary<Guid, ObservableList<KeyValuePair<int, string>>>();

        public ChoiceEditSettings()
            : base()
        {
            this.DisplayMember = "Value";
            this.ValueMember = "Value";
        }

        public static readonly DependencyProperty FieldProperty = DependencyProperty.Register("Field", typeof(FieldChoice), typeof(IFieldEditSettings<FieldChoice>), null);
        public static readonly DependencyProperty ChoiceSourceProperty = DependencyProperty.Register("ChoiceSource", typeof(ObservableList<KeyValuePair<int, string>>), typeof(ComboBoxEditSettings), null);

        public ClientContext SharepointContext
        {
            get;
            set;
        }

        public FieldChoice Field
        {
            get
            {
                return GetValue(FieldProperty) as FieldChoice;
            }
            set
            {
                SetValue(FieldProperty, value);
                InitializeFromField();
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Field"));
                }
            }
        }

        public ObservableList<KeyValuePair<int, string>> ChoiceSource
        {
            get
            {
                return GetValue(ChoiceSourceProperty) as ObservableList<KeyValuePair<int, string>>;
            }
            set
            {
                SetValue(ChoiceSourceProperty, value);
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("ChoiceSource"));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void InitializeFromField()
        {
            ChoiceSource = FetchChoices(Field);
        }

        public override DevExpress.Xpf.Editors.IBaseEdit CreateEditor(bool assignEditorSettings, IDefaultEditorViewInfo defaultViewInfo)
        {
            DevExpress.Xpf.Editors.ComboBoxEdit ret = base.CreateEditor(assignEditorSettings, defaultViewInfo) as DevExpress.Xpf.Editors.ComboBoxEdit;

            Binding binding = new Binding("ChoiceSource");
            binding.Source = this;
            ret.SetBinding(DevExpress.Xpf.Editors.ComboBoxEdit.ItemsSourceProperty, binding);

            //ret.EditValueChanged += new DevExpress.Xpf.Editors.EditValueChangedEventHandler(ret_EditValueChanged);

            return ret;
        }

        //void ret_EditValueChanged(object sender, DevExpress.Xpf.Editors.EditValueChangedEventArgs e)
        //{
        //}


        //    //editor = new UserTagEditor();
        //    //if (assignEditorSettings)
        //    //{
        //    //    AssignToEdit(editor, defaultViewInfo);
        //    //}
        //    editor = base.CreateEditor(assignEditorSettings, defaultViewInfo) as ComboBoxEdit;
        //    //Binding binding = new Binding("Column");
        //    //binding.Converter = new UserTagItemsConverter();
        //    //binding.ConverterParameter = this.Name;
        //    //editor.SetBinding(ComboBoxEdit.ItemsSourceProperty, binding);
        //    editor.Loaded += new RoutedEventHandler(editor_Loaded);
        //    return editor;
        //}

        //void editor_Loaded(object sender, RoutedEventArgs e)
        //{
        //    //if (editor.EditValue != null)
        //    //{
        //    //    editor.ItemsSource = new FieldUserValue[] { editor.EditValue as FieldUserValue };
        //    //}
        //    //editor.Items.Add(editor.DataContext
        //}

        public static ObservableList<KeyValuePair<int, string>> FetchChoices(FieldChoice field)
        {
            Guid fieldId = field.Id;
            if (fieldChoiceCache.ContainsKey(fieldId))
            {
                return fieldChoiceCache[fieldId];
            }
            ObservableList<KeyValuePair<int, string>> olkvp = new ObservableList<KeyValuePair<int, string>>();
            fieldChoiceCache.Add(fieldId, olkvp);

            XDocument xDoc = XDocument.Parse(field.SchemaXml);

            foreach (var xE in xDoc.Element("Field").Element("MAPPINGS").Elements("MAPPING"))
            {
                KeyValuePair<int, string> kvp = new KeyValuePair<int,string>(
                    xE.AttributeValue<int>("Value"),
                    xE.Value);
                olkvp.Add(kvp);
            }

            return olkvp;
        }

    }

    //public class UserTagItemsConverter : IValueConverter
    //{

    //    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    //    {
    //        //RowPropertyValueConverter rowValueConverter = new RowPropertyValueConverter();
    //        //var obj = (rowValueConverter as IValueConverter).Convert(value, null, parameter, culture);

    //        //FieldUserValue fuv = value as FieldUserValue;
    //        //if (fuv != null)
    //        //{
    //        //    if (string.IsNullOrEmpty(parameter as string))
    //        //    {
    //        //        parameter = Attribute;
    //        //    }
    //        //    switch (parameter as string)
    //        //    {
    //        //        default:
    //        //            break;
    //        //        case "LookupId":
    //        //            return fuv.LookupId;
    //        //        case "LookupValue":
    //        //            return fuv.LookupValue;
    //        //    }
    //        //}
    //        return null;
    //    }

    //    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    //    {
    //        return null;
    //    }

    //}

}
